#include "coutchunk.h"

/**
 * COutChunk - constructor
 * @param id identification of chunk
 */
COutChunk::COutChunk(unsigned short id) {
  this->id = id;
  init();
}
//---------------------------------------------------------------------------

/**
 * COutChunk - copy constructor
 * @param chunk
 */
COutChunk::COutChunk(const COutChunk &chunk) {
  this->copyFrom(chunk);
}
//---------------------------------------------------------------------------

/**
 * operator= - assignment (copy) of chunk
 * @param chunk
 */
COutChunk& COutChunk::operator = (const COutChunk &chunk) {
  this->copyFrom(chunk);
  return *this;
}
//---------------------------------------------------------------------------

/**
 * init - initialization
 */
void COutChunk::init() {
  length = sizeof(length) + sizeof(id);
}
//---------------------------------------------------------------------------

/**
 * saveToStream - saves this chunk to data stream
 * @param fh
 * @return num of bytes written into output stream 'fh'
 */
unsigned int COutChunk::saveToStream(ostream &fh) {
  return writeChunk(fh, *this);
}
//---------------------------------------------------------------------------

/**
 * writeChunk - writes chunk into data stream
 * @param fh
 * @param chunk
 * @return pocet zapsanych bytu
 */
unsigned int COutChunk::writeChunk(ostream &fh, const COutChunk &chunk) {

  // Zapise hlavicku vnoreneho chunku
  CBinary::writeUShort(fh, chunk.id);
  CBinary::writeUInt(fh, chunk.length);
  
  // Ulozi obsah datoveho proudu vnoreneho chunku
  fh << chunk.data.rdbuf();
  return chunk.length;
}
//---------------------------------------------------------------------------

/**
 * writeChunk - writes given sub chunk into chunk
 * @param chunk
 */
void COutChunk::writeChunk(const COutChunk &chunk) {
  length += writeChunk(data, chunk);
}
//---------------------------------------------------------------------------

/**
 * writeCColor - writes color in CColor format into chunk
 * @param color
 */
void COutChunk::writeCColor(const CColor &color) {
  length += CBinary::writeCColor(data, color);
}
//---------------------------------------------------------------------------

/**
 * writeString - writes string into chunk
 * @param str
 */
void COutChunk::writeString(const string &str) {
  length += CBinary::writeString(data, str);
}
//---------------------------------------------------------------------------

/**
 * writeBool - writes boolean into chunk
 * @param val
 */
void COutChunk::writeBool(const bool &val) {
  length += CBinary::writeBool(data, val);
}
//---------------------------------------------------------------------------

/**
 * writeDouble - writes double into chunk
 * @param num
 */
void COutChunk::writeDouble(const double &num) {
  length += CBinary::writeDouble(data, num);
}
//---------------------------------------------------------------------------

/**
 * writeFloat - writes float into chunk
 * @param num
 */
void COutChunk::writeFloat(const float &num) {
  length += CBinary::writeFloat(data, num);
}
//---------------------------------------------------------------------------

/**
 * writeShort - writes short integer into chunk
 * @param num
 */
void COutChunk::writeShort(const short &num) {
  length += CBinary::writeShort(data, num);
}
//---------------------------------------------------------------------------

/**
 * writeChar - writes char into chunk
 * @param num
 */
void COutChunk::writeChar(const char &num) {
  length += CBinary::writeChar(data, num);
}
//---------------------------------------------------------------------------

/**
 * writeInt - writes integer into chunk
 * @param num
 */
void COutChunk::writeInt(const int &num) {
  length += CBinary::writeInt(data, num);
}
//---------------------------------------------------------------------------

/**
 * writeUShort - writes unsigned short into chunk
 * @param num
 */
void COutChunk::writeUShort(const unsigned short &num) {
  length += CBinary::writeUShort(data, num);
}
//---------------------------------------------------------------------------

/**
 * writeUChar - writes unsigned char into chunk
 * @param num
 */
void COutChunk::writeUChar(const unsigned char &num) {
  length += CBinary::writeUChar(data, num);
}
//---------------------------------------------------------------------------

/**
 * writeUInt - writes unsigned integer into chunk
 * @param num
 */
void COutChunk::writeUInt(const unsigned int &num) {
  length += CBinary::writeUInt(data, num);
}
//---------------------------------------------------------------------------

/**
 * writeBuffer - writes buffer of given size into chunk
 * @param buf
 * @param bufSize
 */
void COutChunk::writeBuffer(char* buf, unsigned int bufSize) {
  length += CBinary::writeBuffer(data, buf, bufSize);
}
//---------------------------------------------------------------------------
